<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import { getEchartData } from '@/api/codeapi'
import echarts from 'echarts'

require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    }
    /* chartData: {
        type: Object,
        required: true
      }*/
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ expectedData, actualData } = {}) {
      getEchartData({ num: 7,iOrgsysnum:this.$store.getters.iOrgsysnum }).then(res => {
        this.EchartData = res.deviceStatusList // 横坐标时间渲染
        this.expectedData = res.deviceStatusList // 在线设备渲染
        this.actualData = res.deviceStatusList
        const LineEchartData = [] // 横坐标时间     //离线设备渲染
        const LineexpectedData = [] // 在线设备
        const LineactualData = [] // 离线设备
        // 横坐标时间遍历
        this.EchartData.forEach((item, index) => {
          const outObj = {}
          outObj.value = item.strTime
          LineEchartData.push(outObj)
        })
        // 在线设备遍历
        this.expectedData.forEach((item, index) => {
          const outObj = {}
          outObj.value = item.iOnlinecount
          LineexpectedData.push(outObj)
        })
        // 离线设备遍历
        this.actualData.forEach((item, index) => {
          const outObj = {}
          outObj.value = item.iOfflinecount
          LineactualData.push(outObj)
        })
        this.chart.setOption({
          xAxis: {
            /* data: ['06：00', '08：00', '10：00', '12：00', '14：00', '16：00', '18：00'],*/
            data: LineEchartData,
            boundaryGap: false,
            axisTick: {
              show: false
            }
          },
          grid: {
            left: 10,
            right: 10,
            bottom: 20,
            top: 30,
            containLabel: true
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross'
            },
            padding: [5, 10]
          },
          yAxis: {
            axisTick: {
              show: false
            }
          },
          legend: {
            data: ['在线设备', '离线设备']
          },
          series: [
            {
              name: '在线设备', itemStyle: {
                normal: {
                  color: '#FF005A',
                  lineStyle: {
                    color: '#FF005A',
                    width: 2
                  }
                }
              },
              smooth: true,
              type: 'line',
              data: LineexpectedData,
              /* expectedData: [10, 60, 70, 40, 15, 20, 40],  //在线设备数据
                actualData: [10, 52, 30, 30, 12, 30, 30]       //离线设备数据*/
              animationDuration: 2800,
              animationEasing: 'cubicInOut'
            },
            {
              name: '离线设备',
              smooth: true,
              type: 'line',
              itemStyle: {
                normal: {
                  color: '#3888fa',
                  lineStyle: {
                    color: '#3888fa',
                    width: 2
                  },
                  areaStyle: {
                    color: '#f3f8ff'
                  }
                }
              },
              data: LineactualData,
              animationDuration: 2800,
              animationEasing: 'quadraticOut'
            }]
        })
      })
    }
  }
}
</script>
